<template>
  <div class="searchInfo">
    <div class="search-box">
      <div class="search-box-left" @click="noCancel">
        <van-icon name="arrow-left" size="20" />
      </div>
      <div class="search-box-center">
        <van-search
          v-model="value"
          shape="round"
          clearable
          @search="onSearch"
          @input="onInput"
          @focus="onFocus"
        />
      </div>
      <div class="search-box-right" @click="onSearch">搜索</div>
    </div>
    <div class="van-tabs-box">
    <van-tabs v-model="active" color="#fbb705" @click="onswitch" v-if="!isFocus">
      <van-tab
        :title="item.title"
        v-for="(item, index) in typeArr"
        :key="index"
        :title-style="active == index ? 'font-size:16px; font-weight:600' : ''"
      >
        <div class="secondary_keyword" v-if="secondaryKeywordArr.length > 0">
          <div
            class="secondary_keyword-item"
            v-for="(item1, index1) in secondaryKeywordArr"
            :key="index1"
            :class="isactive == index1 ? 'active' : ''"
            @click="onswitch1(item1, index1)"
          >
            {{ item1 }}
          </div>
        </div>
        <div class="content">
          <div
            class="content-item"
            v-for="(item2, index2) in list"
            :key="index2"
            @click="toRecipeDetail(item2.r.id)"
          >
            <div class="content-item-left">
              <img :src="item2.r.img" alt="" />
            </div>
            <div class="content-item-right">
              <div class="name">{{ item2.r.n }}</div>
              <div class="rate" v-if="item2.r.rate != 0">
                {{ item2.r.rate }}分
              </div>
              <div
                class="recipe_list_tags"
                v-if="item2.r.recipe_list_tags.length != 0"
              >
                <div
                  class="recipe_list_tags_item"
                  v-for="(item3, index3) in item2.r.recipe_list_tags"
                  :key="index3"
                >
                  {{ item3.text }}
                </div>
              </div>
              <div class="a">
                <div class="ap">
                  <img :src="item2.r.a.p" alt="" />
                </div>
                <div class="an">{{ item2.r.a.n }}</div>
              </div>
            </div>
          </div>
          <div class="loading" v-if="isloading">
            <van-loading size="24px" color="#fbb705">加载中...</van-loading>
          </div>
        </div>
      </van-tab>
    </van-tabs>
    </div>
    <div class="mask" v-if="isFocus&&value != ''" @click="closeMask"></div>
    <div class="advice" v-if="isFocus&&value != ''">
      <div
        class="advice-item"
        v-for="(item2, index2) in advice"
        :key="index2"
        @click="onSearch1(item2.name)"
      >
        {{ item2.name }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "SearchInfo",
  data() {
    return {
      value: "",
      active: 0,
      isactive: 0,
      order: 0,
      secondary_keyword: "",
      offset: 0,
      isloading: false,
      isFocus:false,
      typeArr: [
        {
          title: "综合排序",
          i: 0,
        },
        {
          title: "收藏最多",
          i: 2,
        },
        {
          title: "学做最多",
          i: 3,
        },
      ],
      secondaryKeywordArr: [],
      list: [],
      advice: [],
    };
  },
  created() {
    this.value = this.$route.query.value;
    this.getSecondaryKeyword();
    this.onSearch();
  },
  methods: {
    noCancel() {
      if(this.isFocus){
        this.isFocus = false
      }else{
        this.$router.go(-1);
      }
    },
    getSecondaryKeyword() {
      this.axios({
        mtehod: "get",
        url: "recipe/search",
        params: {
          keyword: this.value,
        },
      }).then((result) => {
        
        if (result.data.state == "success") {
          this.secondaryKeywordArr = result.data.result.secondary_keywords;
        }
      });
    },
    onSearch() {
      this.isloading = true;
      this.axios({
        mtehod: "get",
        url: "recipe/search",
        params: {
          keyword: this.value,
          order: this.order,
          secondary_keyword: this.secondary_keyword,
          offset: this.offset,
        },
      }).then((result) => {
        
        if (result.data.state == "success") {
          result.data.result.list.forEach((ele) => {
            if (ele.type == 13) {
              ele.r.rate = ele.r.rate.toFixed(1);
              this.list.push(ele);
            }
          });
        }
        this.isloading = false;
        this.offset += 20;
      });
    },
    onFocus(){
      if(this.value == ''){
        return
      }
      this.isFocus = true
      this.axios({
            method: "get",
            url: "recipe/search/suggests",
            params: {
              kw: this.value,
            },
          }).then((result) => {
            
            if (result.data.state == "success") {
              this.advice = result.data.result.suggests;
              console.log(this.advice);
            }
          });
    },
    onInput() {
      setTimeout(() => {
        if (this.value != "") {
          this.isFocus = true
          this.axios({
            method: "get",
            url: "recipe/search/suggests",
            params: {
              kw: this.value,
            },
          }).then((result) => {
            
            if (result.data.state == "success") {
              this.advice = result.data.result.suggests;
              console.log(this.advice);
            }
          });
        }else{
          this.isFocus = false
        }
      }, 200);
    },
    onswitch() {
      
      this.offset = 0;
      this.order = this.typeArr[this.active].i;
      this.list = [];
      this.onSearch();
    },
    onswitch1(item, index) {
      this.isactive = index;
      this.offset = 0;
      if (index == 0) {
        this.secondary_keyword = "";
      } else {
        this.secondary_keyword = item;
      }
      this.list = [];
      this.onSearch();
    },
    loadingMore() {
      this.onSearch();
    },
    toRecipeDetail(id){
      this.$router.push({name:'RecipeDetail',query:{id}})
    },
    closeMask(){
      this.isFocus = false
    },
    onSearch1(name){
      console.log(name);
      this.$router.push({name:'SearchInfo',query:{value:name}})
      location.reload()
      this.isFocus = false
    }
  },
};
</script>

<style lang="less" scoped>
.searchInfo {
  background-color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  .van-tabs-box{
    position: fixed;
    top: 84px;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #fff;
    overflow-y: auto;
    /deep/.van-tabs__wrap{
      position: fixed;
      top: 84px;
      left: 0;
      right: 0;
    }
  }
  .search-box {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: 30px;
    background-color: #fbb705;
    .search-box-left {
      width: 30px;
      height: 54px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .search-box-center {
      width: calc(100% - 30px - 40px);
    }
    .van-search{
      background-color: #fbb705;
    }
    .search-box-right {
      width: 40px;
      font-size: 15px;
    }
  }
  .secondary_keyword {
    display: flex;
    padding: 10px 0 10px 20px;
    background-color: #fff;
    position: fixed;
    top: 128px;
    left: 0;
    right: 0;
    overflow-x: auto;
    .secondary_keyword-item {
      padding: 2px 10px;
      border-radius: 20px;
      background-color: #eee;
      white-space: nowrap;
      margin-right: 15px;
      color: #999;
      &.active {
        background-color: #fbb705;
        color: #000;
        font-weight: 600;
      }
    }
  }
  .content {
    width: calc(100% - 40px);
    // height: calc(667px - 54px - 44px);
    padding: 84px 20px 0 20px;
    // overflow-y: auto;
    .content-item {
      width: 100%;
      display: flex;
      margin: 10px 0;
      .content-item-left {
        img {
          width: 160px;
          height: 130px;
          border-radius: 5px;
        }
      }
      .content-item-right {
        margin-left: 10px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        div {
          margin: 3px 0;
        }
        .rate {
          color: #666;
        }
        .recipe_list_tags {
          display: flex;
          .recipe_list_tags_item {
            background-color: #eee;
            padding: 0 5px;
            color: #666;
            border-radius: 5px;
            margin-right: 5px;
          }
        }
        .a {
          display: flex;
          align-items: center;
          .ap {
            margin-right: 5px;
            img {
              width: 20px;
              height: 20px;
              border-radius: 50%;
            }
          }
        }
      }
    }
    .more {
      width: 100%;
      padding: 10px 0;
      text-align: center;
      font-size: 15px;
    }
    .loading {
      width: 100%;
      margin-top: 10px;
      text-align: center;
    }
  }
  .mask{
   position: fixed;
   top: 0;
   bottom: 0;
   left: 0;
   right: 0;
   z-index: -1;
  }
  .advice {
    width: calc(100% - 40px);
    padding: 0 20px;
    .advice-item {
      font-size: 16px;
      border-bottom: 1px solid #eee;
      padding: 10px 0;
    }
  }
}
</style>